/*
 *pdflib测试代码:
 *原作者的demo程序
 *Created on: 2019-7-9
 *Author: jun
 */
#include "spdf_ext.h"
#include "dataproc.h"
#include "math.h"
#include "str_ext.h"
#include "stdio.h"

extern SPDF_Doc pdf;
extern SPDF_Page page;
static short cnt;
extern SPDF_Doc_Rec pdf_doc;

////////////////////////////////////////////////////////////////////////////////
spdf_chart_ytxt_type y_txt[2] =
	{0};

// 横线列表
const spdf_chart_hl_type crt_hl[4] = {
	{{50, 68, 16, 80}, 1000, 30, 20, 30, 20, "FT TEMP"},
	{{50, 20, 52, 32}, 2500, 30, 20},
	{{50, 12, 48, 64}, 1210, 30, 20, 30, 20, "STRI TEMP"},
};

// 垂线列表
const spdf_chart_vl_type crt_xfg[4] = {
	{32, 30, 5, 30, 15, {30, 20, "HEAT"}},
	{192, 30, 7, 30, 15, {30, 20, "KEEP"}, {30, 20, "VALN"}},
	{256, 30, 5, 30, 15},
	{512, 30, 5, 30, 15},
};

spdf_chart_line_type line_cfg[2] = {0};

ii16 line_val_list[1024] = {0};

void spdf_chart_demo(ii16 dir, ii16 xzoom, ii16 yzoom)
{
	ii16 cnt;
	char cbuff[120];
	spdf_chart_type chart = {0};
	dir = dat16_rang_mod(0, 3, dir);

	loop(4)
	{
		page = spdf_add_page(pdf);

		if (0 == index)
		{
			sprintf(cbuff, "chart dir: %d", dir);
			spdf_bookmark_add(0, 500, 500, cbuff, SPDF_ENCODING_ASCII);
		}
		sprintf(cbuff, "chart dir: %d-%d", dir, index);
		spdf_bookmark_add(1, 500, 500, cbuff, SPDF_ENCODING_ASCII);

		spdf_page_font_set(page, "Courier-Bold", 200);
		spdf_page_color_fill_cmyk_set(page, 20, 5, 5, 0);
		spdf_page_text_begin(page);
		sprintf(cbuff, "ROTATING: %d, X-ZOOM: %d%%, Y-ZOOM: %d%%", dir * 90, xzoom, yzoom);
		spdf_page_text_move(page, 20, 10);
		spdf_page_text_show(page, cbuff);
		// i32_to_string(cbuff, 300, 0x40, 3);
		// spdf_page_text_show(page, cbuff);
		spdf_page_text_end(page);

		// 曲线页面挂载
		spdf_chart_mount(&chart, page);

		// 区域配置
		switch (dir)
		{
		case 0:
			chart.x0 = 450;
			chart.y0 = 450;
			spdf_page_set_width(page, 297 * 3); // 0
			spdf_page_set_height(page, 210 * 3);
			break;
		case 1:
			chart.x0 = 2100 * 3 - 450;
			chart.y0 = 450;
			spdf_page_set_width(page, 210 * 3); // 90
			spdf_page_set_height(page, 297 * 3);

			break;
		case 2:
			chart.x0 = 2970 * 3 - 450;
			chart.y0 = 2100 * 3 - 450;
			spdf_page_set_width(page, 297 * 3); // 180°
			spdf_page_set_height(page, 210 * 3);
			break;

		case 3:
			chart.x0 = 450;
			chart.y0 = 2970 * 3 - 450;
			spdf_page_set_width(page, 210 * 3); // 270
			spdf_page_set_height(page, 297 * 3);
			break;
		default:
			chart.x0 = 450;
			chart.y0 = 450;
			spdf_page_set_width(page, 297 * 3); // 0
			spdf_page_set_height(page, 210 * 3);
			break;
		}

		spdf_color_fill_rgb(98, 100, 100);
		spdf_rectangle(0, 0, spdf_page_get_width(page), spdf_page_get_height(page), 0x40);
		spdf_stroke_fill();

		chart.height = 5000;
		chart.width = 8000;
		chart.xzoom = 4096 * xzoom / 100;
		chart.yzoom = 4096 * yzoom / 100;
		chart.dir = dir; // 曲线方向，仅0有坐标文本

		// 轴及网格配置
		chart.frame.mode = 1 + (index & 1); // 0-仅轴，1-网格，2刻度
		chart.frame.scale_m_size = 60;		// 刻度模式，主刻度长度
		chart.frame.scale_s_size = 25;		// 刻度模式，子刻度长度
		chart.frame.grid_x_m_wide = 500;	// x向，主网格间距
		chart.frame.grid_y_m_wide = 500;	// y向，主网格间距
		chart.frame.grid_x_s_num = 5;		// x向，子网格数量
		chart.frame.grid_y_s_num = 5;		// y向，子网格数量
		chart.frame.arrows_offset = 70;		// 箭头位置，若无箭头时非零补充右上边框
		chart.frame.arrows_size = 50;		// 箭头尺寸，设为0无箭头
		if (chart.frame.mode == 1)
		{
			spdf_chart_axis(&chart, 100, 8, 8, 28);	   // 轴-线宽，RGB
			spdf_chart_grid_m(&chart, 30, 90, 90, 90); // 主网格-线宽，RGB
			spdf_chart_grid_s(&chart, 15, 95, 95, 95); // 子网格-线宽，RGB
		}
		else
		{
			spdf_chart_axis(&chart, 100, 8, 8, 28);	 // 轴-线宽，RGB
			spdf_chart_grid_m(&chart, 50, 8, 8, 28); // 主网格-线宽，RGB
			spdf_chart_grid_s(&chart, 25, 8, 8, 28); // 子网格-线宽，RGB
		}

		// 空间横线
		chart.frame.hl = crt_hl;				 // 线配置
		chart.frame.hl_amount = arr_len(crt_hl); // 数量

		// 生成线数据
		for (cnt = 0; cnt < sizeof(line_val_list) / sizeof(line_val_list[0]); cnt++)
		{
			float site;
			site = (float)10 * cnt * 3.14 / (sizeof(line_val_list) / sizeof(line_val_list[0]));
			line_val_list[cnt] = (ii16)(sin(site) * 1000);
		}

		// 线配置
		line_cfg[0].attr.wide = 50; // 线宽
		line_cfg[0].attr.r = 12;	// 线颜色
		line_cfg[0].attr.g = 8;
		line_cfg[0].attr.b = 48;
		line_cfg[0].point_val_list = line_val_list; // 数据列表
		line_cfg[0].point_amount = 1024;			// 数据个数
		line_cfg[0].axis_x_split_nums = 1000;		// x轴坐标增量，每点宽度
		line_cfg[0].point_val_coef = 1UL << 12;		// 数据值系数
		line_cfg[0].point_val_offset = 0;			// 数据偏移
		line_cfg[0].axis_y_offset = 2500;			// y值偏移
		line_cfg[0].point_mode = 0;					// 0-值模式  1-点模式
		line_cfg[0].point_pos_offset = 0;			// 值模式起点偏移点数
		line_cfg[0].v_l = crt_xfg;					// 垂线配置，颜色继承主线颜色
		line_cfg[0].v_l_amount = 4;					// 垂线数量

		// 线配置
		line_cfg[1].attr.wide = 50;
		line_cfg[1].attr.r = 44;
		line_cfg[1].attr.g = 4;
		line_cfg[1].attr.b = 4;
		line_cfg[1].point_val_list = line_val_list;
		line_cfg[1].point_amount = 512;
		line_cfg[1].axis_x_split_nums = 800;
		line_cfg[1].point_val_coef = 1UL << 10;
		line_cfg[1].point_val_offset = 0;
		line_cfg[1].axis_y_offset = 1000;
		line_cfg[1].point_mode = 0;
		line_cfg[1].point_pos_offset = 100;
		line_cfg[1].v_l = crt_xfg;	// 垂线配置，颜色继承主线颜色
		line_cfg[1].v_l_amount = 2; // 垂线数量

		// x轴文本配置
		chart.txt.x_mode = index;	// 模式-//0-数值 //1-rtc-tm // 2-rtc/td //3-rtc/dt
		chart.txt.x_val_ipn = 0x40; // 值整数小数
		chart.txt.x_val_start = 0;	// 值起始值
		chart.txt.x_val_inc = 30;	// 值/时间增量
		chart.txt.x_r = 8;			// 文本颜色
		chart.txt.x_g = 8;
		chart.txt.x_b = 28;
		chart.txt.x_rtc.time.year = 20; // 起始时间
		chart.txt.x_rtc.time.mon = 8;
		chart.txt.x_rtc.time.day = 15;
		chart.txt.x_rtc.time.hour = 23;
		chart.txt.x_rtc.time.min = 30;
		chart.txt.xtxt_dx = 0;
		chart.txt.xtxt_dy = -10;

		// Y轴文本配置 - 多文本
		y_txt[0].r = 44;
		y_txt[0].g = 4;
		y_txt[0].b = 4;
		y_txt[0].ipn = 0x32;
		y_txt[0].unit = "K";
		y_txt[0].val_start = -2000;
		y_txt[0].val_inc = 1000;
		y_txt[0].val_attr = 0x10; // 值转换属性，参考i32_to_string

		y_txt[1].b = 12; // 颜色
		y_txt[1].g = 8;
		y_txt[1].b = 48;
		y_txt[1].ipn = 0x40;	// 值格式
		y_txt[1].unit = "RH";	// 后缀
		y_txt[1].val_start = 0; // 开始值
		y_txt[1].val_inc = 500; // 值增量

		chart.txt.ytxt = y_txt;	 // Y文本配置列表
		chart.txt.y_txt_num = 2; // Y轴文本数量
		chart.txt.ytxt_dx = -20;
		chart.txt.ytxt_dy = 10;

		// 轴文本统配
		chart.txt.font_size = 80; // 字号
		chart.txt.font_wide = 40; // 半字宽度
		chart.txt.font_name = 0;  // 字体，为0时，默认SunSim
		chart.txt.enable = 1;	  // 开关

		// 曲线配置 - 多线
		chart.line_list = line_cfg;
		chart.line_num = 2;

		spdf_chart_show(&chart);

		spdf_color_fill_rgb(44, 4, 4);
		spdf_color_line_rgb(44, 4, 4);
		spdf_chart_ext_move_to(&chart, 500, 4950);
		spdf_chart_ext_line_to(&chart, 800, 4950);
		spdf_stroke();
		spdf_text_begin();
		spdf_chart_ext_show_text(&chart, 810, 4950, 0, -30, "TEMP");
		spdf_text_end();

		spdf_color_fill_rgb(12, 8, 48);
		spdf_color_line_rgb(12, 8, 48);
		spdf_chart_ext_move_to(&chart, 1200, 4950);
		spdf_chart_ext_line_to(&chart, 1500, 4950);
		spdf_stroke();
		spdf_text_begin();
		spdf_chart_ext_show_text(&chart, 1510, 4950, 0, -30, "PRESS");
		spdf_text_end();

		spdf_page_save(page);
	}
}
